var e = require('../@babel/runtime/helpers/regeneratorRuntime.js'),
  t = require('../@babel/runtime/helpers/asyncToGenerator.js'),
  r = (getApp(), 0);
module.exports = Behavior({
  properties: {},
  options: {},
  data: { P_rpx: 1, P_dpr: 1, P_imgs: {} },
  pageLifetimes: {},
  created: function () {},
  attached: function () {
    var e = wx.getSystemInfoSync(),
      t = e.screenWidth / 750,
      r = e.pixelRatio;
    (this.data.P_rpx = t * r),
      (this.data.P_dpr = r),
      this.setData({ P_rpx: t * r, P_dpr: r });
  },
  ready: function () {},
  methods: {
    _doDraw: function () {
      var r = this,
        a = this.data._list;
      return new Promise(
        (function () {
          var n = t(
            e().mark(function t(n, i) {
              var o, c, s;
              return e().wrap(function (e) {
                for (;;)
                  switch ((e.prev = e.next)) {
                    case 0:
                      (o = []), (c = 0);
                    case 2:
                      if (!(c < a.length)) {
                        e.next = 12;
                        break;
                      }
                      return (
                        (s = a[c]), (e.t0 = o), (e.next = 7), r.drawOne(s, c)
                      );
                    case 7:
                      (e.t1 = e.sent), e.t0.push.call(e.t0, e.t1);
                    case 9:
                      c++, (e.next = 2);
                      break;
                    case 12:
                      Promise.all(o)
                        .then(function (e) {
                          n(e);
                        })
                        .catch(function (e) {
                          console.error(e), i(e);
                        });
                    case 13:
                    case 'end':
                      return e.stop();
                  }
              }, t);
            }),
          );
          return function (e, t) {
            return n.apply(this, arguments);
          };
        })(),
      );
    },
    drawOne: function (a, n) {
      var i = this;
      return t(
        e().mark(function t() {
          var o;
          return e().wrap(function (e) {
            for (;;)
              switch ((e.prev = e.next)) {
                case 0:
                  return (
                    i,
                    (o = i.data._temp_static),
                    e.abrupt(
                      'return',
                      new Promise(function (e, t) {
                        try {
                          wx.createSelectorQuery()
                            .in(i)
                            .select('#canvas_'.concat(n))
                            .fields({ node: !0, size: !0 })
                            .exec(function (t) {
                              var n = i.data.P_dpr,
                                c = {
                                  bg_b: o.b_bg.temp_url,
                                  logo: o.logo.temp_url,
                                  qr_code: a.temp_qr_url,
                                  thumb: a.temp_goods_thumb,
                                  line: './line.png',
                                },
                                s = t[0].width,
                                u = t[0].height,
                                l = t[0].node,
                                d = l.getContext('2d');
                              (l.width = s * n), (l.height = u * n);
                              l.requestAnimationFrame(function t() {
                                clearTimeout(r),
                                  (r = i.render(l, d, a)
                                    ? setTimeout(function () {
                                        wx.canvasToTempFilePath(
                                          {
                                            canvas: l,
                                            quality: 1,
                                            success: function (t) {
                                              e(t.tempFilePath),
                                                wx.hideLoading();
                                            },
                                            fail: function (e) {
                                              console.error(
                                                'canvasToTempFilePath complete:',
                                                e,
                                              );
                                            },
                                          },
                                          i,
                                        );
                                      }, 100)
                                    : setTimeout(function () {
                                        l.requestAnimationFrame(t);
                                      }, 100));
                              }),
                                i.getImgs(l, c, i);
                            });
                        } catch (e) {
                          t(e);
                        }
                      }),
                    )
                  );
                case 3:
                case 'end':
                  return e.stop();
              }
          }, t);
        }),
      )();
    },
    render: function (e, t, r) {
      var a = [];
      return (
        a.push(this.drawBG(e, t)),
        a.push(this.drawThumb(e, t)),
        a.push(this.drawQR(e, t)),
        a.push(this.drawLine(e, t)),
        a.push(this.drawLogo(e, t, r)),
        a.push(this.drawText(e, t, r)),
        !!a.every(function (e) {
          return e;
        })
      );
    },
    drawBG: function (e, t) {
      var r = this.data,
        a = r.P_imgs,
        n = r.P_rpx;
      if (!a.bg_b) return !1;
      var i = a.bg_b;
      return (
        t.drawImage(i, 0, 0, i.width, i.height, 0, 0, 750 * n, 1172 * n), !0
      );
    },
    drawThumb: function (e, t) {
      var r = this.data,
        a = r.P_imgs,
        n = r.P_rpx;
      if (!a.thumb) return !1;
      var i = a.thumb;
      return (
        t.drawImage(
          i,
          0,
          0,
          i.width,
          i.height,
          50 * n,
          101 * n,
          648 * n,
          622 * n,
        ),
        !0
      );
    },
    drawQR: function (e, t) {
      var r = this.data,
        a = r.P_imgs,
        n = r.P_rpx;
      if (!a.qr_code) return !1;
      var i = a.qr_code;
      return (
        t.drawImage(
          i,
          0,
          0,
          i.width,
          i.height,
          601 * n,
          745 * n,
          95 * n,
          95 * n,
        ),
        !0
      );
    },
    drawLine: function (e, t) {
      var r = this.data,
        a = r.P_imgs,
        n = r.P_rpx;
      if (!a.line) return !1;
      var i = a.line;
      return (
        t.drawImage(
          i,
          0,
          0,
          i.width,
          i.height,
          67 * n,
          900 * n,
          626 * n,
          3 * n,
        ),
        !0
      );
    },
    drawLogo: function (e, t) {
      var r = this.data,
        a = r.P_imgs,
        n = r.P_rpx;
      if (!a.logo) return !1;
      var i = a.logo;
      return (
        t.drawImage(
          i,
          0,
          0,
          i.width,
          i.height,
          216 * n,
          858 * n,
          233 * n,
          242 * n,
        ),
        !0
      );
    },
    drawText: function (e, t, r) {
      var a = this.data.P_rpx;
      t.beginPath(),
        (t.font = 'bold '.concat(30 * a, 'px PingFangSC-Regular, PingFang SC')),
        (t.fillStyle = 'rgba(1, 1, 1, 1)'),
        this.canvas_dealWords({
          ctx: t,
          font: 'bold '.concat(30 * a, 'px PingFangSC-Regular, PingFang SC'),
          lineHeight: 40 * a,
          word: r.goods_title,
          maxWidth: 490 * a,
          x: 64 * a,
          y: 739 * a,
          maxLine: 2,
        }),
        (t.fillStyle = 'rgba(113, 112, 112, 1)'),
        (t.font = '400 '.concat(24 * a, 'px PingFangSC-Regular, PingFang SC')),
        t.fillText(r.sku_properties_name, 64 * a, 868 * a),
        (t.font = '400 '.concat(20 * a, 'px PingFangSC-Regular, PingFang SC')),
        (t.fillStyle = 'rgba(80, 80, 80, 1)'),
        t.fillText('长按浏览商品', 590 * a, 869 * a),
        (t.font = '400 '.concat(20 * a, 'px PingFangSC-Regular, PingFang SC')),
        (t.fillStyle = 'rgba(140, 140, 140, 1)'),
        t.fillText('收货人', 67 * a, 974 * a),
        t.fillText('购入金额', 67 * a, 1026 * a),
        t.fillText('下单时间', 67 * a, 1078 * a);
      var n = parseInt(
          t.measureText(
            ''.concat(r.receiver_name, '  ').concat(r.receiver_mobile),
          ).width,
        ),
        i = parseInt(t.measureText('¥'.concat(r.actual_fee)).width),
        o = parseInt(t.measureText(r.pay_time).width),
        c = e.width;
      return (
        t.fillText(
          ''.concat(r.receiver_name, '  ').concat(r.receiver_mobile),
          c - n - 56 * a,
          974 * a,
        ),
        t.fillText('¥'.concat(r.actual_fee), c - i - 56 * a, 1026 * a),
        t.fillText(r.pay_time, c - o - 56 * a, 1078 * a),
        t.restore(),
        !0
      );
    },
    getImgs: function (e, t, r) {
      var a = function (a) {
        if (Object.prototype.hasOwnProperty.call(t, a)) {
          var n = t[a],
            i = e.createImage();
          (i.onload = function () {
            r.data.P_imgs[a] = i;
          }),
            (i.src = n || '/images/potu.png'),
            (i.onerror = function (e) {
              (i.src = '/images/potu.png'),
                console.error(
                  '获取图片时出现错误。若多次出现此提示，请截图反馈给开发，key为'
                    .concat(a, '; 其值为')
                    .concat(n, '; \nerr: ')
                    .concat(e),
                ),
                wx.hideLoading(),
                wx.showModal({
                  title: '获取图片资源异常',
                  content: e,
                  showCancel: !1,
                });
            });
        }
      };
      for (var n in t) a(n);
    },
    canvas_dealWords: function (e) {
      (e.maxLine = e.maxLine ? e.maxLine : 2),
        (e.lineHeight = e.lineHeight ? e.lineHeight : 34),
        (e.quotes = e.quotes || !1),
        (e.outType = e.outType || '...'),
        (e.word = e.word || '');
      var t = 1;
      e.word.length > 0 && (e.word = e.quotes ? '“'.concat(e.word) : e.word),
        (e.ctx.font = e.font);
      for (
        var r = Math.ceil(e.ctx.measureText(e.word).width / e.maxWidth),
          a = r >= e.maxLine ? e.maxLine : r,
          n = 0,
          i = 0;
        i < a;
        i++
      ) {
        var o = e.word.slice(n),
          c = 0;
        if (e.ctx.measureText(o).width > e.maxWidth) {
          for (var s = 0; s < o.length; s++)
            if ((c += e.ctx.measureText(o[s]).width) > e.maxWidth) {
              i === e.maxLine - 1
                ? e.ctx.fillText(
                    o.slice(0, s - 1) +
                      ''.concat(e.quotes ? '...”' : e.outType),
                    e.x,
                    e.y + (i + 1) * e.lineHeight,
                  )
                : e.ctx.fillText(
                    o.slice(0, s),
                    e.x,
                    e.y + (i + 1) * e.lineHeight,
                  ),
                (n += s),
                (t = i + 1);
              break;
            }
        } else {
          var u = e.quotes ? ''.concat(o.slice(0), '”') : o.slice(0);
          e.ctx.fillText(u, e.x, e.y + (i + 1) * e.lineHeight), (t = i + 1);
        }
      }
      return t;
    },
    canvas_dealWords_one: function (e) {
      var t = e.outType ? e.outType : '...';
      (e.word = e.word || ''), (e.ctx.font = e.font);
      var r = '',
        a = !1;
      if (e.ctx.measureText(e.word).width <= e.maxWidth) (r = e.word), (a = !1);
      else
        for (var n = 0; n < e.word.length; n++)
          e.ctx.measureText(e.word.slice(0, n)).width <= e.maxWidth &&
            ((r = e.word.slice(0, n)), (a = e.word.length - 1 >= n));
      e.ctx.fillText(r + ''.concat(a ? t : ''), e.x, e.y);
    },
  },
});
